package com.bjpowernode.service.impl;

import com.alibaba.excel.EasyExcel;
import com.bjpowernode.config.listener.UploadDataListener;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.mapper.TClueMapper;
import com.bjpowernode.model.TActivity;
import com.bjpowernode.model.TClue;
import com.bjpowernode.model.TUser;
import com.bjpowernode.query.ClueQuery;
import com.bjpowernode.service.ClueService;
import com.bjpowernode.util.JWTUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ClueServiceImpl implements ClueService {

    @Resource
    private TClueMapper tClueMapper;

    @Override
    public PageInfo<TClue> getClueByPage(Integer current) {
        //1.设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //2.查询
        List<TClue> list = tClueMapper.selectCluePage();
        //3.封装分页数据到PageInfo
        PageInfo<TClue> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void importExcel(InputStream inputStream, String jwt) {
        EasyExcel.read(inputStream, TClue.class, new UploadDataListener(tClueMapper, jwt))
                .sheet()
                .doRead();
    }

    @Override
    public Boolean checkPhone(String phone) {
        int count = tClueMapper.selectCountByPhone(phone);
        return count > 0; //返回true表示手机号存在，false表示手机号不存在
    }

    /**
     * 录入线索（提交保存）
     *
     * @param clueQuery
     * @return
     */
    @Override
    public int saveClue(ClueQuery clueQuery) {
        TClue tClue = new TClue();

        //需要把ClueQuery数据复制到TClue对象中
        //spring提供了一个工具类：BeanUtils，它可以实现把一个对象的属性值复制到另一个对象中
        //复制的要求：源对象和目标对象，两个对象的成员变量名称相同，成员变量的类型也相同，这样才可以复制
        BeanUtils.copyProperties(clueQuery, tClue);

        //设置一下创建时间
        tClue.setCreateTime(new Date());

        //设置一下创建人
        Integer loginUserId = JWTUtil.parseUserFromJWT(clueQuery.getJwt()).getId();
        tClue.setCreateBy(loginUserId);

        return tClueMapper.insertSelective(tClue);
    }

    @Override
    public TClue getClueById(Integer id) {
        return tClueMapper.selectClueDetailById(id);
    }

    /**
     * 编辑线索（提交保存）
     *
     * @param clueQuery
     * @return
     */
    @Override
    public int updateClue(ClueQuery clueQuery) {
        TClue tClue = new TClue();

        //需要把ActivityQuery数据复制到TActivity对象中
        //spring提供了一个工具类：BeanUtils，它可以实现把一个对象的属性值复制到另一个对象中
        //复制的要求：源对象和目标对象，两个对象的成员变量名称相同，成员变量的类型也相同，这样才可以复制
        BeanUtils.copyProperties(clueQuery, tClue);

        //设置一下编辑时间
        tClue.setEditTime(new Date());

        //设置一下编辑人
        Integer loginUserId = JWTUtil.parseUserFromJWT(clueQuery.getJwt()).getId();
        tClue.setEditBy(loginUserId);

        return tClueMapper.updateByPrimaryKeySelective(tClue);
    }

    @Override
    public int delClueById(Integer id) {
        return tClueMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchDelClueById(List<String> idList) {
        return tClueMapper.deleteByIds(idList);
    }
}
